package zhieasy.cn.service.found.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhieasy.cn.common.utils.ResultVOUtil;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.found.FoundCommentRoot;
import zhieasy.cn.model.found.LafCategory;
import zhieasy.cn.model.found.LostAndFind;
import zhieasy.cn.persistence.mapper.found.LafCategoryMapper;
import zhieasy.cn.persistence.mapper.found.LostAndFindMapper;
import zhieasy.cn.service.found.LafService;

import java.util.List;

/**
 * @author wangzeya
 * @create 2020-08-02 22:22
 */
@Service
public class LafServiceImpl implements LafService {

    @Autowired
    private LafCategoryMapper lafCategoryMapper;
    @Autowired
    private LostAndFindMapper lostAndFindMapper;

    @Autowired
    private FoundCommentRootServiceImpl foundCommentRootService;

    /**
     * 查找所有分类
     *
     * @return
     */
    @Override
    public ResultVO<LafCategory> findAllCategory() {
        List<LafCategory> lafCategories = lafCategoryMapper.selectList(null);
        return ResultVOUtil.success(lafCategories);
    }

    /**
     * 创建失物招领
     *
     * @param lostAndFind
     * @return
     */
    @Override
    public ResultVO creatLaf(LostAndFind lostAndFind) {
        lostAndFindMapper.insert(lostAndFind);
        return ResultUtils.success();
    }

    /**
     * 查询失物招领列表
     *
     * @param pageNum  当前页码
     * @param pageSize 每页显示条数
     * @return
     */
    @Override
    public ResultVO<IPage<LostAndFind>> findLafList(int pageNum, int pageSize, int lafType, int lafCtgId) {

//        QueryWrapper<LostAndFind> wrapper = new QueryWrapper<>();
//        wrapper.eq("laf_active",0);
//        wrapper.eq("laf_type",lafType);
//        wrapper.eq("laf_ctg_id",lafCtgId);
//        IPage<LostAndFind> lostAndFindPage = new Page<>(pageNum, pageSize);//参数一是当前页，参数二是每页个数
//        IPage<LostAndFind> lostAndFindIPage = lostAndFindMapper.selectPage(lostAndFindPage, wrapper);
        List<LostAndFind> lafList ;
        if (lafCtgId == -1){
            lafList = lostAndFindMapper.findLafListWithAnyCateId(pageSize * (pageNum - 1), pageSize * pageNum, lafType);
        }else {
            lafList = lostAndFindMapper.findLafListByCateId(pageSize * (pageNum - 1), pageSize * pageNum, lafType, lafCtgId);
        }

        return ResultVOUtil.success(lafList);
    }

    /**
     * 获取失物招领详细信息
     *
     * @param lafId 失物招领id
     * @return
     */
    @Override
    public ResultVO<LostAndFind> findLafDetail(String lafId) {
        LostAndFind lafDetail = lostAndFindMapper.findLafByOpenId(lafId);
        if (lafDetail == null) {
            return ResultVOUtil.error(1111, "用id：" + lafId + "查询lostandfind，找不到失物招领信息");
        }
        List<FoundCommentRoot> commentRootList = foundCommentRootService.getCommonRootByFoundId(lafDetail.getId(), 1, 5);
        lafDetail.setFoundCommentRoots(commentRootList);
        return ResultVOUtil.success(lafDetail);
    }


    @Override
    public ResultVO<IPage<LostAndFind>> findLafByLafName(int pageNum, int pageSize, String key) {
        IPage<LostAndFind> lostAndFindPage = new Page<>(pageNum, pageSize);//参数一是当前页，参数二是每页个数
        QueryWrapper<LostAndFind> queryWrapper = new QueryWrapper<>();
        queryWrapper.or(
            wrapper->
                wrapper.like("laf_ctg_name",key)
                    .or().like("laf_name",key)
                    .or().like("laf_detail",key)
                    .or().like("laf_text",key)
                    .or().like("laf_type",key)
        );
        queryWrapper.orderByDesc("laf_create_time");
        IPage<LostAndFind> lostAndFindIPage = lostAndFindMapper.selectPage(lostAndFindPage, queryWrapper);
        List<LostAndFind> records = lostAndFindIPage.getRecords();
        return ResultVOUtil.success(records);

    }


}
